package com.wjf.birdfish.util;

import java.io.File;
import java.io.IOException;

/****
 * 文件操作辅助类
 * 
 * @author lzp
 * 
 */
public class FileUtil {
	/****
	 * 路径转换成统一格式
	 * @param path
	 * @return
	 */
	public static String fixPath(String path) {
		if (path == null)
			return "";
		if (path.indexOf('\\') >= 0) {
			path = path.replace('\\', '/');
		}
		int len = path.length();
		if ((len > 0) && (path.charAt(len - 1) == '/')) {
			path = path.substring(0, len - 1);
		}
		return path;
	}

	/***
	 * 得到文件的扩展名
	 * @param filePath
	 * @return
	 */
	public static String getFileExtension(String filePath) {
		int index = filePath.lastIndexOf('.');
		if ((index < 0) || (index == filePath.length()))
			return null;
		return filePath.substring(index + 1);
	}

	/****
	 * 删除文件及子文件
	 * @param file
	 */
	public static void deleteFileAndEmptyDirectory(File file) {
		if (!file.exists()) {
			return;
		}
		if (file.isDirectory()) {
			File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
			for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
				deleteFileAndEmptyDirectory(files[i]); // 把每个文件 用这个方法进行迭代
			}
			file.delete();
		} else if (file.isFile()) {
			file.delete();
		}
		
	}
	
	/***
	 * 删除文件及子文件
	 * @param path
	 */
	public static void deleteFileAndEmptyDirectory(String path) {
		deleteFileAndEmptyDirectory(new File(path));
	}

	/***
	 * 创建文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static File createPathAndFile(String path) throws IOException {
		if (path == null) {
			return null;
		}
		path = fixPath(path);
		int rootStart = path.indexOf("/");
		String rootPath = path.substring(0, rootStart);
		String subPath = path.substring(rootStart + 1);

		if (subPath.indexOf('/') < 0) {
			File file = new File(rootPath);
			file.createNewFile();
			return file;
		}

		String[] parts = subPath.split("/");
		String nowPath = rootPath;
		File file = null;
		for (int i = 0; i < parts.length - 1; i++) {
			nowPath = nowPath + "/" + parts[i];
			file = new File(nowPath);
			if (file.isDirectory()) {
				continue;
			}
			file.mkdir();
		}

		file = new File(path);
		file.createNewFile();
		return file;
	}
	
	/***
	 * 创建文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static File createPath(String path) throws IOException {
		if (path == null) {
			return null;
		}
		path = fixPath(path);
		int rootStart = path.indexOf("/");
		String rootPath = path.substring(0, rootStart);
		String subPath = path.substring(rootStart + 1);

		if (subPath.indexOf('/') < 0) {
			File file = new File(rootPath);
			return file;
		}

		String[] parts = subPath.split("/");
		String nowPath = rootPath;
		File file = null;
		for (int i = 0; i < parts.length - 1; i++) {
			nowPath = nowPath + "/" + parts[i];
			file = new File(nowPath);
			if (file.isDirectory()) {
				continue;
			}
			file.mkdir();
		}

		file = new File(path);
		return file;
	}
	
	public static void main(String[] args){
		deleteFileAndEmptyDirectory("F:/srm_tmp/upload/valid/AccpHisSer");
	}
}
